package jp.co.systemi.asp.workflow.webservice;

import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.logging.Logger;

import org.apache.wicket.Component;
import org.apache.wicket.behavior.IBehavior;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.WebPage;

/**
 * RESTful WebSevice page
 */
public abstract class WebServicePage extends WebPage {
    private static final Logger log = Logger.getLogger(WebServicePage.class
            .getName());

    private static final long serialVersionUID = 1L;

    public WebServicePage() {
        setStatelessHint(true);
    }

    protected final void onRender(MarkupStream markupStream) {
        getResponse().setCharacterEncoding("UTF-8");
//        getResponse().setContentType("text/xml;charset=UTF-8");
        PrintWriter pw = null;
        try {
            pw = new PrintWriter(new OutputStreamWriter(getResponse()
                    .getOutputStream(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            log.info(e.getLocalizedMessage());
        }
        String xml = getXML();
        log.info("output xml: " + xml);
        pw.write(xml);
        pw.close();
    }

    protected abstract String getXML();

    @Override
    public final String getMarkupType() {
        return "xml";
    }

    @Override
    public final boolean hasAssociatedMarkup() {
        return false;
    }

    @Override
    public final Component add(IBehavior... behaviors) {
        return null;
        // throw new UnsupportedOperationException(
        // "WebServicePage does not support IBehaviours");
    }
}
